package com.csii.adhoc.board.serviceImpl;

import com.csii.adhoc.board.BoardPo;
import com.csii.adhoc.board.mapper.BoardMapper;
import com.csii.adhoc.board.service.BoardService;
import com.csii.adhoc.dataready.TMgnDirPo;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.id.UidGenerator;
import com.csii.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @description: 仪表盘接口实现
 * @author: panChuanJiang
 * @create: 2021-02-02 15:29
 */
@Service("boardService")
@Slf4j
public class BoardServiceImpl implements BoardService {
    @Resource
    private BoardMapper boardMapper;
    @Resource
    private UidGenerator uidGenerator;

    @Override
    @Transactional
    public void boardMaintain(BoardPo boardPo, String optMode) {
        switch (optMode) {
            case CommonConstant.OPT_MODE_ADD:
                boardPo.setCreatedBy(boardPo.getUpdatedBy())
                        .setCreatedDt(boardPo.getUpdatedDt());
                insertOneBoardPo(boardPo);
                break;
            case CommonConstant.OPT_MODE_DELETE:
                deleteBoardPoByPrimaryKey(boardPo.getBoardId());
                break;
            case CommonConstant.OPT_MODE_UPDATE:
                updateBoardPoByPrimaryKey(boardPo);
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("optMode值不合法:" + optMode);
        }
    }

    @Transactional
    @Override
    public void addBoardFromDir(TMgnDirPo tMgnDirPo) {
        BoardPo boardPo = new BoardPo();
        boardPo.setCreatedDt(tMgnDirPo.getCreatedDt())
                .setCreatedBy(tMgnDirPo.getCreatedBy())
                .setUpdatedDt(tMgnDirPo.getUpdatedDt())
                .setUpdatedBy(tMgnDirPo.getUpdatedBy())
                .setBoardId(tMgnDirPo.getMenuId())
                .setName(tMgnDirPo.getName())
                .setIsTop("2")
                .setSeqno(new BigDecimal(tMgnDirPo.getSeqno()))
                .setType("3");
        insertOneBoardPo(boardPo);
    }

    @Override
    @Transactional
    public void updateBoardPoByPrimaryKey(BoardPo boardPo) {
        boardMapper.updateBoardPoByPrimaryKey(boardPo);
    }

    @Transactional
    public void deleteBoardPoByPrimaryKey(String boardId) {
        boardMapper.deleteBoardPoByPrimaryKey(boardId);
    }

    @Transactional
    public void insertOneBoardPo(BoardPo boardPo) {
        if (StringUtils.isEmpty(boardPo.getBoardId())) {
            boardPo.setBoardId(uidGenerator.getUID() + "");
        }
        boardMapper.insertOneBoardPo(boardPo);
    }

    @Transactional
    @Override
    public List<BoardPo> selectListBoard(BoardPo boardPo) {
        return boardMapper.selectListBoard(boardPo);
    }
}
